﻿using Bootstrap.DataAccess;
using Bootstrap.Security;
using Longbow.Web.Mvc;
using System.Linq;

namespace Bootstrap.Admin.Query
{
    /// <summary>
    /// 
    /// </summary>
    public class QueryAnswerOption : PaginationOption
    {
        /// <summary>
        /// 
        /// </summary>
        public string IP { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Phone { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public QueryData<object> RetrieveData()
        {
            var data = AnswerHelper.Retrieves();
            if (!string.IsNullOrEmpty(IP))
            {
                data = data.Where(t =>                    
                        (string.IsNullOrEmpty(t.IP) ? "" : t.IP).Contains(IP)                    
                        );
            }
            if (!string.IsNullOrEmpty(Phone))   
            {
                data = data.Where(
                    a=>  (string.IsNullOrEmpty(a.Phone)?"": a.Phone).Contains(Phone)
                        );
            }
           
            var ret = new QueryData<object>();
            ret.total = data.Count();
            // 通过option.Sort属性判断对那列进行排序
            switch (Sort)
            {
              
                case "IP":
                    data = Order == "asc" ? data.OrderBy(t => t.IP) : data.OrderByDescending(t => t.IP);
                    break;
             
                default:
                    break;
            }
            ret.rows = data.Skip(Offset).Take(Limit);
            return ret;
        }
    }
}